package com.example.psychologicalappointmentsystem.JpaRepository;

import com.example.psychologicalappointmentsystem.Entity.OrderDO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface OrderRepository extends JpaRepository<OrderDO, Long> {

    Page<OrderDO> findAll(Pageable pageable);

    //分页查询咨询师的订单
    Page<OrderDO> findByConsultantId(Long id,Pageable pageable);

    //查询用户的订单
    Page<OrderDO> findByUserId(Long id,Pageable pageable);

    @Query(nativeQuery = true,value = "select * from consultation_order o where o.user_id = :user_id and status = 'RESERVED' order by created_date desc limit 1")
    OrderDO findByUserIdLast(@Param("user_id") Long userId);

    @Query(value = "SELECT * FROM consultation_order o WHERE o.consultant_id = :consultant_id AND o.start_time >= :start_time AND o.end_time <= :end_time AND o.status = 'RESERVED'",nativeQuery = true)
    List<OrderDO> findOrdersByTimeFrame(@Param("consultant_id") Long consultantId,
                                        @Param("start_time") Date startTime,
                                        @Param("end_time") Date endTime);

}
